package leetcode.code0649;

import java.util.LinkedList;
import java.util.Queue;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {
	int R = 'R' - 'D';
	int D = 0;

	public String predictPartyVictory(String senate) {
		int r = 0, d = 0;
		Queue<Integer> queue = new LinkedList<>();
		char[] cs = senate.toCharArray();
		for (int i = 0; i < cs.length; i++) {
			int cur = cs[i] - 'D';
			if (cur == R) {
				if (d > 0) {
					d--;
				} else {
					r++;
					queue.add(cur);
				}
			} else {
				if (r > 0) {
					r--;
				} else {
					d++;
					queue.add(cur);
				}
			}
		}
		while (!queue.isEmpty()) {
			int size = queue.size();
			if (r >= size) {
				return "Radiant";
			} else if (d >= size) {
				return "Dire";
			}
			while (size > 0) {
				int cur = queue.poll();
				if (cur == R) {
					if (d > 0) {
						d--;
					} else {
						r++;
						queue.add(cur);
					}
				} else {
					if (r > 0) {
						r--;
					} else {
						d++;
						queue.add(cur);
					}
				}
				size--;
			}
		}
		return null;
	}

	@Override
	public void debug4() {
		H.compare("Radiant", this.predictPartyVictory("RDR"));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
